package com.woniuxy.woniuticket.searchmodule.service;


import com.woniuxy.woniuticket.pojo.CinemaInfo;
import com.woniuxy.woniuticket.pojo.CinemaMovies;
import com.woniuxy.woniuticket.searchmodule.dto.SearchDto;
//import com.woniuxy.woniuticket.searchmodule.repository.MoviesInforRepository;
//import com.woniuxy.woniuticket.searchmodule.repository.MoviesRepository;
import com.woniuxy.woniuticket.searchmodule.repository.MoviesInforRepository;
import com.woniuxy.woniuticket.searchmodule.repository.MoviesRepository;
import com.woniuxy.woniuticket.searchmodule.repository.SearchDtoRepository;
import com.woniuxy.woniuticket.searchmodule.util.SearchParameterUtil;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.SearchResultMapper;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/*搜索业务逻辑层*/
@Service
public class SearchService {
    @Resource
    private MoviesRepository moviesRepository;

    @Autowired
    private MoviesInforRepository moviesInforRepository;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    /*数据展示*/
    public List<CinemaMovies> showSer(){
        SearchDto searchDto = new SearchDto();
        MatchAllQueryBuilder allQueryBuilders = QueryBuilders.matchAllQuery();
        Iterable<CinemaMovies>cinemaMoviesIterable = this.moviesRepository.search(allQueryBuilders);
        List<CinemaMovies> cinemaMoviesList = new ArrayList<>();
        for (CinemaMovies cinemaMovies:
                cinemaMoviesIterable) {
            cinemaMoviesList.add(cinemaMovies);
        }
        return  cinemaMoviesList;
    }
    /*动态搜索框提示*/
    public SearchDto searchMovie(SearchParameterUtil searchParameterUtil){
        //搜索条件
        String key = searchParameterUtil.getKey();
        SearchDto searchDto = new SearchDto();
        //名字自定义分词搜索
        QueryBuilder moveisQueryBuilder = QueryBuilders.matchQuery("movename",key).analyzer("ik_max_word");
        //地址自定义搜索
        QueryBuilder infoQueryBuilder = QueryBuilders.matchQuery("address",key).analyzer("ik_max_word");
        //接受名字集合
        Iterable<CinemaMovies> cinemaMoviesIterable = this.moviesRepository.search(moveisQueryBuilder);
        //接受地址集合
        Iterable<CinemaInfo>cinemaInfoIterable = this.moviesInforRepository.search(infoQueryBuilder);
        //list接受
        List<CinemaMovies> cinemaMoviesList = new ArrayList<>();
        List<CinemaInfo>cinemaInfoList = new ArrayList<>();
        //CinemaMovies cinemaMoviesIn = this.moviesRepository.findByID(1);
       // System.out.println(cinemaMoviesIn.toString());
        for (CinemaMovies cinemaMovies:
                cinemaMoviesIterable) {
            cinemaMoviesList.add(cinemaMovies);
        }
        searchDto.setCinemaMovies(cinemaMoviesList);
        for (CinemaInfo cinemaInfo:
                cinemaInfoIterable) {
            cinemaInfoList.add(cinemaInfo);
        }
        //添加到DTO类中
        searchDto.setCinemaInfo(cinemaInfoList);
        System.out.println(searchDto.getCinemaMovies().toString()+"qqq");
        System.out.println(searchDto.getCinemaInfo().toString()+"ssss");
        System.out.println(searchDto.toString()+"aaaaa");
        return  searchDto;
    }

    /*高亮查询*/
/*
    public SearchDto highLigthQuery(SearchParameterUtil searchParameterUtil) {
        //搜索条件
        String key = searchParameterUtil.getKey();
        SearchDto searchDto = new SearchDto();
        //名字自定义分词搜索
        QueryBuilder moveisQueryBuilder = QueryBuilders.matchQuery("movename",key).analyzer("ik_max_word");
        //地址自定义搜索
        QueryBuilder infoQueryBuilder = QueryBuilders.matchQuery("address",key).analyzer("ik_max_word");
        //接受名字集合
        Iterable<CinemaMovies> cinemaMoviesIterable = this.moviesRepository.search(moveisQueryBuilder);
        //接受地址集合
        Iterable<CinemaInfo>cinemaInfoIterable = this.moviesInforRepository.search(infoQueryBuilder);
        //list接受
        List<CinemaMovies> cinemaMoviesList = new ArrayList<>();
        List<CinemaInfo>cinemaInfoList = new ArrayList<>();
        //遍历添加接受
        for (CinemaMovies cinemaMovies:
                cinemaMoviesIterable) {
            cinemaMoviesList.add(cinemaMovies);
        }
        searchDto.setCinemaMovies(cinemaMoviesList);
        for (CinemaInfo cinemaInfo:
                cinemaInfoIterable) {
            cinemaInfoList.add(cinemaInfo);
        }
        //设置高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder().field("*").requireFieldMatch(false);
        highlightBuilder.preTags("<span style\"color:red\">");
        highlightBuilder.postTags("</span>");

        //添加到DTO类中
        searchDto.setCinemaInfo(cinemaInfoList);
        System.out.println(searchDto.getCinemaMovies().toString()+"qqq");
        System.out.println(searchDto.getCinemaInfo().toString()+"ssss");
        System.out.println(searchDto.toString()+"aaaaa");
        return  searchDto;
    }
*/

    /*public SearchDto highLightQuery(SearchParameterUtil searchParameterUtil){
        String key = searchParameterUtil.getKey();
        Integer start = searchParameterUtil.getStartPage();
        Integer size = searchParameterUtil.getDefaultSize();
        BoolQueryBuilder movieQuery = QueryBuilders.boolQuery();
        movieQuery.should(QueryBuilders.matchQuery("movename",key).analyzer("ik_max_word"))
                  .should(QueryBuilders.matchQuery("mainactor",key).analyzer("ik_max_word"));
        Pageable pageable = PageRequest.of(start,size);
        NativeSearchQuery movieSearchQuery = new NativeSearchQueryBuilder()
                .withQuery(movieQuery)
                .withHighlightFields(new HighlightBuilder.Field("movename"),
                        new HighlightBuilder.Field("mainactor"))
                .withPageable(pageable)
                .build();
        AggregatedPage<SearchDto> searchDtoAggregatedPage= elasticsearchTemplate.queryForPage(movieSearchQuery, SearchDto.class, new SearchResultMapper() {
            @Override
            public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
               SearchDto searchDto = new SearchDto();
                SearchHits hits = response.getHits();
                for (SearchHit hit:
                     hits) {
                    if (hits.totalHits <= 0){
                        return  null;
                    }
                    searchDto
                }
                return null;
            }
        })
    }*/
   /* public List<CinemaMovies> searchMovie(SearchParameterUtil searchParameterUtil){
        String key = searchParameterUtil.getKey();
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQ
        ueryBuilder();
        queryBuilder.withQuery(QueryBuilders.matchQuery("movename" , key).operator(Operator.AND));
        Iterable<CinemaMovies>moviesIterable  =  moviesRepository.search(queryBuilder.build());
        List<CinemaMovies>cinemaMoviesList = new ArrayList<>();
        for (CinemaMovies cinemaMovies:moviesIterable) {
            cinemaMoviesList.add(cinemaMovies);
        }
        return cinemaMoviesList;
    }*/

}
